/***
 * Excerpted from "Stripes: and Java Web Development is Fun Again",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material, 
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose. 
 * Visit http://www.pragmaticprogrammer.com/titles/fdstr for more book information.
***/
package stripesbook.dao.impl.stripersist;

import javax.persistence.EntityManager;
import org.stripesstuff.stripersist.Stripersist;
import stripesbook.dao.MessageDao;
import stripesbook.model.Attachment;
import stripesbook.model.Folder;
import stripesbook.model.Message;

public class MessageDaoImpl extends BaseDaoImpl<Message,Integer>
    implements MessageDao
{
    public void addMessageToFolder(Message message, Folder folder) {
        EntityManager em = Stripersist.getEntityManager();
        em.persist(message);
        message.setFolder(folder);

        for (Attachment attachment : message.getAttachments()) {
            attachment.setMessage(message);
            em.merge(attachment);
        }
    }
    @Override
    public void delete(Message message) {
        EntityManager em = Stripersist.getEntityManager();

        for (Attachment attachment : message.getAttachments()) {
            em.remove(attachment);
        }
        em.remove(message);
    }
}
